package com.diaohw.module.metadata.service.dictionary;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.diaohw.module.metadata.mapper.DictionaryItemMapper;
import com.diaohw.module.metadata.mapper.DictionaryMapper;
import com.diaohw.module.metadata.obj.dto.DictionaryItemDTO;
import com.diaohw.module.metadata.obj.entity.DictionaryDO;
import com.diaohw.module.metadata.obj.entity.DictionaryItemDO;
import com.diaohw.module.metadata.obj.query.DictionaryItemQuery;
import com.diaohw.module.metadata.obj.vo.DictionaryItemVO;
import com.diaohw.module.metadata.service.DictionaryItemService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.diaohw.platform.framework.db.utils.DataCheckUtils;
import com.diaohw.platform.framework.db.utils.WrapperUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 数据字典项信息表 服务实现类
 * </p>
 *
 * @author D-Hw
 * @since 2024-09-18
 */
@Service
public class DictionaryItemImpl extends ServiceImpl<DictionaryItemMapper, DictionaryItemDO> implements DictionaryItemService {

    @Resource
    private DictionaryMapper dictionaryMapper;



    @Override
    public DictionaryItemVO create(DictionaryItemDTO data) {
        DataCheckUtils.eq(DictionaryItemDO::getDictionaryId, data.getDictionaryId(), DictionaryItemDO::getItemName, data.getItemName(), this.baseMapper);
        DataCheckUtils.eq(DictionaryItemDO::getDictionaryId, data.getDictionaryId(), DictionaryItemDO::getItemCode, data.getItemCode(), this.baseMapper);
        DictionaryItemDO model = INSTANCE.dtoToEntity(data);
        DictionaryDO dictionary = dictionaryMapper.selectById(model.getDictionaryId());
        model.setDictionaryCode(dictionary.getDictionaryCode());
        model.setDictionaryName(dictionary.getDictionaryName());
        this.save(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public DictionaryItemVO update(String id, DictionaryItemDTO data) {
        DataCheckUtils.eq(DictionaryItemDO::getDictionaryId, data.getDictionaryId(), DictionaryItemDO::getItemName, data.getItemName(), id, this.baseMapper);
        DataCheckUtils.eq(DictionaryItemDO::getDictionaryId, data.getDictionaryId(), DictionaryItemDO::getItemCode, data.getItemCode(), id, this.baseMapper);
        DictionaryItemDO model = INSTANCE.dtoToEntity(data);
        model.setId(id);
        this.updateById(model);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public Boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    public Boolean deleteByAttributeId(String id) {
        Wrapper<DictionaryItemDO> wrapper = Wrappers.<DictionaryItemDO>lambdaQuery()
                .eq(DictionaryItemDO::getDictionaryId, id);
        return this.remove(wrapper);
    }

    @Override
    public DictionaryItemVO detail(String id) {
        DictionaryItemDO model = this.getById(id);
        return INSTANCE.entityToVo(model);
    }

    @Override
    public List<DictionaryItemVO> findByList(DictionaryItemQuery query) {
        LambdaQueryWrapper<DictionaryItemDO> wrapper = createWrapper(query);
        List<DictionaryItemDO> list = this.list(wrapper);
        return INSTANCE.entityToVo(list);
    }

    @Override
    public PageResultVo<DictionaryItemVO> findByPage(DictionaryItemQuery query) {
        LambdaQueryWrapper<DictionaryItemDO> wrapper = createWrapper(query);
        Page<DictionaryItemDO> page = this.page(WrapperUtils.createPage(query), wrapper);
        List<DictionaryItemVO> list = INSTANCE.entityToVo(page.getRecords());
        return PageResultVo.create(page.getTotal(), list);
    }

    @Override
    public List<DictionaryItemVO> findByDictionaryId(List<String> dictionaryIds) {
        LambdaQueryWrapper<DictionaryItemDO> wrapper = Wrappers.lambdaQuery(DictionaryItemDO.class);
        wrapper.in(DictionaryItemDO::getDictionaryId, dictionaryIds);
        return INSTANCE.entityToVo(this.list(wrapper));
    }

    /**
     * 生成查询条件
     *
     * @param query 查询条件
     */
    private LambdaQueryWrapper<DictionaryItemDO> createWrapper(DictionaryItemQuery query) {
        LambdaQueryWrapper<DictionaryItemDO> wrapper = Wrappers.lambdaQuery(DictionaryItemDO.class);
        wrapper.like(StrUtil.isNotBlank(query.getItemName()), DictionaryItemDO::getItemName, query.getItemName());
        wrapper.like(StrUtil.isNotBlank(query.getItemCode()), DictionaryItemDO::getItemCode, query.getItemCode());
        wrapper.like(StrUtil.isNotBlank(query.getDescription()), DictionaryItemDO::getDescription, query.getDescription());
        wrapper.eq(ObjUtil.isNotNull(query.getDisplay()), DictionaryItemDO::getDisplay, query.getDisplay());
        wrapper.like(StrUtil.isNotBlank(query.getDictionaryId()), DictionaryItemDO::getDictionaryId, query.getDictionaryId());
        wrapper.like(StrUtil.isNotBlank(query.getDictionaryCode()), DictionaryItemDO::getDictionaryCode, query.getDictionaryCode());
        wrapper.like(StrUtil.isNotBlank(query.getRemark()), DictionaryItemDO::getRemark, query.getRemark());
        return wrapper;
    }

}
